﻿using System;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using ORM;

namespace Concret
{
/// <summary>
/// 用户
/// </summary>
public class Users : DataObject
{
public Users(DbOperation dbOperation) => base.dbOp = dbOperation;

	/// <summary>
	/// 数据表
	/// </summary>
	public override string TABLE_NAME => "users";
	/// <summary>
	/// 数据表对Sql适配
	/// </summary>
	public string SQL_TABLE => dbOp.DBCode(TABLE_NAME);
	/// <summary>
	/// 数据表注释
	/// </summary>
	public override string TABLE_COMMENT => "用户";
	
	#region 字段
#region 帐号
/// <summary>
/// “帐号”在数据库的字段名
/// </summary>
public const string FIELD_ACCOUNT = "account", DESCR_ACCOUNT = "帐号";
/// <summary>
/// “account”对Sql适配
/// </summary>
public string SQL_ACCOUNT => dbOp.DBCode(FIELD_ACCOUNT);
/// <summary>
/// “帐号”在类中的属性名
/// </summary>
public const string VAR_ACCOUNT = "Account_Val";
string account_val;
/// <summary>
/// “帐号”值
/// </summary>
public string Account_Val {get => account_val; set {FVs[FIELD_ACCOUNT]=value; if(value!=account_val) ChangedFVs[FIELD_ACCOUNT]=account_val=value;}}
#endregion

#region 密码
/// <summary>
/// “密码”在数据库的字段名
/// </summary>
public const string FIELD_PASSWORD = "password", DESCR_PASSWORD = "密码";
/// <summary>
/// “password”对Sql适配
/// </summary>
public string SQL_PASSWORD => dbOp.DBCode(FIELD_PASSWORD);
/// <summary>
/// “密码”在类中的属性名
/// </summary>
public const string VAR_PASSWORD = "Password_Val";
string password_val;
/// <summary>
/// “密码”值
/// </summary>
public string Password_Val {get => password_val; set {FVs[FIELD_PASSWORD]=value; if(value!=password_val) ChangedFVs[FIELD_PASSWORD]=password_val=value;}}
#endregion

#region 姓名
/// <summary>
/// “姓名”在数据库的字段名
/// </summary>
public const string FIELD_UNAME = "uname", DESCR_UNAME = "姓名";
/// <summary>
/// “uname”对Sql适配
/// </summary>
public string SQL_UNAME => dbOp.DBCode(FIELD_UNAME);
/// <summary>
/// “姓名”在类中的属性名
/// </summary>
public const string VAR_UNAME = "Uname_Val";
string uname_val;
/// <summary>
/// “姓名”值
/// </summary>
public string Uname_Val {get => uname_val; set {FVs[FIELD_UNAME]=value; if(value!=uname_val) ChangedFVs[FIELD_UNAME]=uname_val=value;}}
#endregion

#endregion

		#region 单表查询
		/// <summary>
		/// 根据this已填充或刷新的属性来查询数据(使用参数查询机制)。为了简化设计与兼顾灵活性，所有条件关系为单层单连接，如果要混合组合和优先级，则从结果中用linq Where过滤或选择GetBySqlWhere
		/// </summary>
		/// <param name="and_or">and/or</param>
		/// <param name="maxCount">结果最多记录数(默认无限制)</param>
		/// <param name="orders">结果排序设置</param>
		/// <returns>对象集合</returns>
		public new IEnumerable<Users> GetByFVs(string and_or = "and", int maxCount = 0, params (string field, string order)[] orders)
		=> base.GetByFVs(and_or, maxCount, orders).Cast<Users>();

		/// <summary>
		/// 查询(不使用参数查询机制)
		/// </summary>
		/// <param name="sqlWhere">查询条件where子句</param>
		/// <param name="maxCount">结果最多记录数(默认无限制)</param>
		/// <param name="orders">结果排序设置</param>
		/// <returns>对象集合</returns>
		public new IEnumerable<Users> GetBySqlWhere(string sqlWhere = null, int maxCount = 0, params (string field, string order)[] orders)
		=> base.GetBySqlWhere(sqlWhere, maxCount, orders).Cast<Users>();

		/// <summary>
		/// 查询(使用参数查询机制)
		/// </summary>
		/// <param name="conditions">查询条件。为了简化设计与兼顾灵活性，所有条件关系为单层单连接，如果要混合组合和优先级，则从结果中用linq Where过滤或选择GetBySqlWhere</param>
		/// <param name="and_or">and/or</param>
		/// <param name="maxCount">结果最多记录数(默认无限制)</param>
		/// <param name="orders">结果排序设置</param>
		/// <returns>对象集合</returns>
		public new IEnumerable<Users> GetByParameter(IEnumerable<(string field, string op, object value)> conditions = null, string and_or = "and", int maxCount = 0, params (string field, string order)[] orders)
		=> base.GetByParameter(conditions, and_or, maxCount, orders).Cast<Users>();

		/// <summary>
		/// 查询(使用参数查询机制，与重载的GetByParameter一致)
		/// </summary>
		/// <param name="fieldVals">查询条件。为了简化设计与兼顾灵活性，所有条件关系为单层单连接，如果要混合组合和优先级，则从结果中用linq Where过滤或选择GetBySqlWhere</param>
		/// <returns>对象集合</returns>
		public new IEnumerable<Users> GetByParameter(params (string field, string op, object value)[] fieldVals)
		=> base.GetByParameter(conditions: fieldVals).Cast<Users>();

		/// <summary>
		/// 根据Id来获取记录
		/// </summary>
		/// <param name="Id">id值</param>
		/// <returns>存在返回对应那条记录，否则为null</returns>
		public new Users GetById(long Id)
		=> base.GetById(Id) as Users;
		#endregion

		#region 获取分页记录
		/// <summary>
		/// 根据this已填充或刷新的属性来获取分页数据(使用参数查询机制)。为了简化设计与兼顾灵活性，所有条件关系为单层单连接，如果要混合组合和优先级，则选择PageBySqlWhere
		/// </summary>
		/// <param name="indexBasedOne">页码(首页为1)</param>
		/// <param name="perPage">每页记录数</param>
		/// <param name="and_or">and/or</param>
		/// <param name="orders">排序设置(默认id降序)</param>
		/// <returns>对象集合</returns>
		public new IEnumerable<Users> PageByFVs(int indexBasedOne = 1, int perPage = 10, string and_or = "and", params (string field, string order)[] orders)
		=> base.PageByFVs(indexBasedOne, perPage, and_or, orders).Cast<Users>();

		/// <summary>
		/// 分页(不使用参数查询机制)
		/// </summary>
		/// <param name="indexBasedOne">页码(首页为1)</param>
		/// <param name="perPage">每页记录数</param>
		/// <param name="sqlWhere">查询条件where子句</param>
		/// <param name="orders">排序设置(默认id降序)</param>
		/// <returns>对象集合</returns>
		public new IEnumerable<Users> PageBySqlWhere(int indexBasedOne = 1, int perPage = 10, string sqlWhere = null, params (string field, string order)[] orders)
		=> base.PageBySqlWhere(indexBasedOne, perPage, sqlWhere, orders).Cast<Users>();

		/// <summary>
		/// 分页(使用参数查询机制)
		/// </summary>
		/// <param name="indexBasedOne">页码(首页为1)</param>
		/// <param name="perPage">每页记录数</param>
		/// <param name="conditions">查询条件。为了简化设计与兼顾灵活性，所有条件关系为单层单连接，如果要混合组合和优先级，则选择PageBySqlWhere</param>
		/// <param name="and_or"></param>
		/// <param name="orders">排序设置(默认id降序)</param>
		/// <returns>对象集合</returns>
		public new IEnumerable<Users> PageByParameter(int indexBasedOne = 1, int perPage = 10, IEnumerable<(string field, string op, object value)> conditions = null, string and_or = "and", params (string field, string order)[] orders)
		=> base.PageByParameter(indexBasedOne, perPage, conditions, and_or, orders).Cast<Users>();

		/// <summary>
		/// 分页(使用参数查询机制，与重载的PageByParameter一致)
		/// </summary>
		/// <param name="fieldVals">查询条件。为了简化设计与兼顾灵活性，所有条件关系为单层单连接，如果要混合组合和优先级，则选择PageBySqlWhere</param>
		/// <returns>对象集合</returns>
		public new IEnumerable<Users> PageByParameter(params (string field, string op, object value)[] fieldVals)
		=> base.PageByParameter(conditions: fieldVals).Cast<Users>();
		#endregion
}
}